package com.ke.jun.view.widght

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

/**
 * @author:      tainzhi
 * @mail:        qfq61@qq.com
 * @date:        2020/8/22 22:57
 * @description:
 **/

class CircleMaskView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
        private var paint = Paint().apply {
                isAntiAlias = true
                style = Paint.Style.STROKE
                strokeWidth = 20f
                color = Color.RED
        }
        
        private var fillPaint = Paint().apply {
                isAntiAlias = true
                color = Color.YELLOW
        }
        
        private var center = 0f
        private var radius = 0f
        private var ringWidth = 10f
        private var path = Path()
        private lateinit var rectF: RectF
        
        override fun onDraw(canvas: Canvas) {
                super.onDraw(canvas)
                canvas.drawCircle(center, center, radius, paint)
                paint.run {
                        alpha = 0
                        setXfermode(PorterDuffXfermode(PorterDuff.Mode.SRC_IN))
                }
            canvas.drawCircle(center, center, radius - ringWidth, paint)
                canvas.drawPath(getLeftField(), fillPaint)
                canvas.drawPath(getRightField(), fillPaint)
                canvas.drawPath(getBottomField(), fillPaint)
        }
        
        override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec)
                val width = MeasureSpec.getSize(widthMeasureSpec)
                val height = MeasureSpec.getSize(heightMeasureSpec)
                center = width/2f
                radius = width/2f
                rectF = RectF(0F, 0f, width.toFloat(), width.toFloat())
                
        }
        
        private fun getLeftField(): Path {
                return path.apply {
                        reset()
                        moveTo(0f, 0f)
                        lineTo(0f, center)
                        arcTo(rectF, 180f, 90f)
                        lineTo(0f, 0f)
                        close()
                }
        }
        
        private fun getRightField(): Path {
                return path.apply {
                        reset()
                        moveTo(center, 0f)
                        arcTo(rectF, 270f, 90f)
                        lineTo(center *2, 0f)
                        lineTo(center, 0f)
                        close()
                }
        }
        
        private fun getBottomField(): Path {
                return path.apply {
                        reset()
                        moveTo(center*2, center)
                        arcTo(rectF, 0f, 180f)
                        lineTo(0f, height.toFloat())
                        lineTo(center *2, height.toFloat())
                        lineTo(center *2, center)
                        close()
                }
        }
}